package com.yixin.my.proj.web.controller;

import com.yixin.my.proj.common.AppConstant;
import com.yixin.my.proj.domain.User;
import com.yixin.my.proj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("system")
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping(value="main",method= RequestMethod.GET)
    public String main(){
        return "main";
    }


    @RequestMapping(value="login",method= RequestMethod.GET)
    public String login(){
        return "login";
    }

    @RequestMapping(value="login",method= RequestMethod.POST)
    public String login(User user, Model model, HttpServletRequest request){

        if (user.getUsername() != null && user.getPassword() != null){
            User goodUser = userService.login(user);
            if (goodUser != null){
                //登入成功
                //吧TbUser放入session
                request.getSession().setAttribute(AppConstant.SESSION_USER,goodUser);
                return "redirect:main";
            }

        }
        //登入失败
        model.addAttribute("message","用户名或者密码输入错误！");
        return login();
    }
}
